package com.gin.no_gay_alliance.database.entity;

import androidx.annotation.NonNull;
import androidx.room.Entity;
import androidx.room.Ignore;
import androidx.room.Index;
import androidx.room.PrimaryKey;

import com.gin.jackson.utils.ObjectUtils;
import com.gin.nga.response.alterlog.LastEdit;
import com.gin.nga.response.field.AlterInfo;
import com.gin.no_gay_alliance.viewmodel.ReplyItem;

import java.time.ZonedDateTime;
import java.util.List;
import java.util.Locale;

/**
 * 回复历史
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/9/16 11:31
 **/
@Entity(indices = {
        //分页索引, 用于浏览模式查询
        @Index(name = "topicId", value = {"topicId", "floorNumber", "uuid"})
        // 回复Id索引, 用于历史版本查询
        , @Index(name = "replyId", value = {"replyId", "uuid"})
        // 搜索索引，用于回复动态查询
        , @Index(name = "search", value = {"postDatetime"})
})
public class ReplyHistory {
    /**
     * 主键，版本标识（回复Id+最后修改时间）
     */
    @PrimaryKey
    @NonNull
    public String uuid;
    /**
     * 本记录更新时间
     */
    public ZonedDateTime timestamp;
    /**
     * 回复id
     */
    public long replyId;
    /**
     * 回复正文
     */
    public String content;
    /**
     * 楼层号
     */
    public int floorNumber;
    /**
     * 由楼层计算得到的页数
     */
    public int page;
    /**
     * 作者uid
     */
    public long authorUid;
    /**
     * 用户名
     */
    public String username;
    /**
     * 标题
     */
    public String title;
    /**
     * 主题id
     */
    public long topicId;
    /**
     * 是否匿名
     */
    public boolean anonymous;
    /**
     * 修改记录（原始数据）
     */
    public String alterinfoRaw;
    /**
     * 发表日期
     */
    public ZonedDateTime postDatetime;

    /**
     * 历史版本
     */
    @Ignore
    public List<ReplyHistory> versions;

    public ReplyHistory() {
    }

    public ReplyHistory(ReplyItem that) {
        this.replyId = that.replyId;
        this.content = that.content;
        this.floorNumber = that.floorNumber != null ? that.floorNumber : 0;
        this.authorUid = that.authorUid;
        this.username = that.username;
        this.title = that.title;
        this.topicId = that.topicId;
        this.anonymous = that.anonymous;
        this.alterinfoRaw = that.alterinfoRaw;
        this.postDatetime = that.postDatetime;


        // 计算字段

        // 最后修改时间
        this.uuid = String.format(Locale.CHINA, "%d_%d", this.replyId, findLastEdit().toEpochSecond());
        // 记录更新时间
        this.timestamp = ZonedDateTime.now();
        // 所在页
        this.page = this.floorNumber / 20 + 1;
    }

    /**
     * 返回最后修改时间
     *
     * @return 最后修改时间
     */
    private ZonedDateTime findLastEdit() {
        if (ObjectUtils.isEmpty(alterinfoRaw)) {
            return postDatetime;
        }
        final String s = alterinfoRaw.replace("||", "");
        AlterInfo alterInfo = new AlterInfo(s);
        LastEdit lastEdit = alterInfo.getLastEdit();
        if (lastEdit == null) {
            return postDatetime;
        }
        return lastEdit.getTimestamp();
    }
}
